class_name InputHandler
extends Node

# 输入处理器 - 分离输入处理和状态逻辑，支持能力限制

signal movement_input(direction: Vector2)
signal jump_pressed()
signal jump_released()
signal dash_pressed()
signal dash_released()
signal interact_pressed()
signal pre_build_pressed()
signal bridge_switch()

@export var character: Character
var input_buffer: Dictionary = {}
var buffer_time: float = 0.1  # 输入缓冲时间

# 输入状态
var is_jump_held: bool = false
var is_dash_held: bool = false
var jump_hold_time: float = 0.0
var dash_hold_time: float = 0.0

# 能力限制状态 - 对应Character中的can_*变量
var can_move: bool = true
var can_use_card: bool = true
var can_dash: bool = true
var can_jump: bool = true
var can_pre_build: bool = true


# 能力限制计时器
var ability_timers: Dictionary = {}
var debug_mode: bool = false

func _ready() -> void:
	if not character:
		character = owner as Character
	
	debug_mode = OS.is_debug_build()
	
	# 连接角色的能力状态
	if character:
		_sync_abilities_from_character()
	
	if debug_mode:
		print("[InputHandler] 输入处理器已初始化，角色: %s" % (character.name if character else "None"))

func _process(delta: float) -> void:
	_handle_movement_input()
	_handle_action_input()
	_update_input_timers(delta)
	_process_input_buffer(delta)

## 返回当前的移动按键向量（不触发信号）
func get_current_movement() -> Vector2:
	if not can_move:
		return Vector2.ZERO
	var direction = Vector2.ZERO
	direction.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	direction.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	return direction

# 处理移动输入
func _handle_movement_input() -> void:
	if not can_move:
		movement_input.emit(Vector2.ZERO)
		return
	
	var direction = Vector2.ZERO
	direction.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	direction.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	
	movement_input.emit(direction)

# 处理动作输入
func _handle_action_input() -> void:
	# 跳跃输入
	if Input.is_action_just_pressed("jump") and can_jump:
		is_jump_held = true
		jump_hold_time = 0.0
		jump_pressed.emit()
		_buffer_input("jump", true)
	
	if Input.is_action_just_released("jump") && can_jump:
		is_jump_held = false
		jump_released.emit()
	
	# 冲刺输入
	if Input.is_action_just_pressed("dash") and can_dash:
		is_dash_held = true
		dash_hold_time = 0.0
		dash_pressed.emit()
		_buffer_input("dash", true)
	
	if Input.is_action_just_released("dash") && can_dash:
		is_dash_held = false
		dash_released.emit()
	
	if Input.is_action_just_pressed("interact"):
		interact_pressed.emit()
	
	# 攻击输入
	if Input.is_action_just_pressed("pre_build") and can_pre_build:
		pre_build_pressed.emit()
		_buffer_input("pre_build", true)
#
	#
	if Input.is_action_just_pressed("1"):
		bridge_switch.emit(0)
	elif Input.is_action_just_pressed("2"):
		bridge_switch.emit(1)
	elif Input.is_action_just_pressed("3"):
		bridge_switch.emit(2)
	elif Input.is_action_just_pressed("4"):
		bridge_switch.emit(3)


# 更新输入计时器
func _update_input_timers(delta: float) -> void:
	if is_jump_held:
		jump_hold_time += delta
	
	if is_dash_held:
		dash_hold_time += delta

# 输入缓冲系统
func _buffer_input(action: String, value: bool) -> void:
	input_buffer[action] = {
		"value": value,
		"time": buffer_time
	}

# 处理输入缓冲
func _process_input_buffer(delta: float) -> void:
	var keys_to_remove: Array[String] = []
	
	for action in input_buffer.keys():
		input_buffer[action].time -= delta
		if input_buffer[action].time <= 0:
			keys_to_remove.append(action)
	
	for key in keys_to_remove:
		input_buffer.erase(key)

# 检查缓冲输入
func has_buffered_input(action: String) -> bool:
	return input_buffer.has(action) and input_buffer[action].value

# 消费缓冲输入
func consume_buffered_input(action: String) -> bool:
	if has_buffered_input(action):
		input_buffer.erase(action)
		return true
	return false

## 获取跳跃按住时间
func get_jump_hold_time() -> float:
	return jump_hold_time if is_jump_held else 0.0

## 获取冲刺按住时间
func get_dash_hold_time() -> float:
	return dash_hold_time if is_dash_held else 0.0

## 检查是否按住跳跃
func is_jump_being_held() -> bool:
	return is_jump_held

## 检查是否按住冲刺
func is_dash_being_held() -> bool:
	return is_dash_held

## 清除所有输入状态
func clear_input_state() -> void:
	is_jump_held = false
	is_dash_held = false
	jump_hold_time = 0.0
	dash_hold_time = 0.0
	input_buffer.clear()

## 设置输入缓冲时间
func set_buffer_time(time: float) -> void:
	buffer_time = max(0.0, time)

# === 能力管理系统 ===

## 同步角色的能力状态
func _sync_abilities_from_character() -> void:
	if not character:
		return
	
	can_move = character.can_move
	can_dash = character.can_dash
	can_jump = character.can_jump

## 设置能力状态
func set_ability(ability_name: String, enabled: bool) -> void:
	match ability_name:
		"move":
			can_move = enabled
		"pre_build":
			can_pre_build = enabled
		"dash":
			can_dash = enabled
		"jump":
			can_jump = enabled
		

	

	
